package com.example.hospital_208.controller;

import com.example.hospital_208.po.Room;
import com.example.hospital_208.service.RoomService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/rooms")
public class RoomController {

    private final RoomService roomService;

    public RoomController(RoomService roomService) {
        this.roomService = roomService;
    }

    // 查询所有房间
    @GetMapping
    public List<Room> getAllRooms() {
        return roomService.listRooms();
    }

    // 根据ID查询房间
    @GetMapping("/{roomId}")
    public Room getRoom(@PathVariable Integer roomId) {
        return roomService.getRoomById(roomId);
    }

    // 新增房间
    @PostMapping
    public String addRoom(@RequestBody Room room) {
        int result = roomService.addRoom(room);
        return result > 0 ? "房间添加成功" : "房间添加失败";
    }

    // 更新房间
    @PutMapping
    public String updateRoom(@RequestBody Room room) {
        int result = roomService.updateRoom(room);
        return result > 0 ? "房间更新成功" : "房间更新失败";
    }

    // 删除房间
    @DeleteMapping("/{roomId}")
    public String deleteRoom(@PathVariable Integer roomId) {
        int result = roomService.deleteRoom(roomId);
        return result > 0 ? "房间删除成功" : "房间删除失败";
    }
}